package com.example.myhelloworld.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ButtonBase;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class plusMinusDivTimes extends ExerciseClass{

	ButtonBase closeButton;

	HTML[] 	questList 		= new HTML[21];
	TextBox[] answerList	= new TextBox[21];
	int[]	correctAnswers	= new int[20];
	Button submitButton		= new Button("Correct");
	boolean freshQuest 		= true;
	String userName 		= "";
	GreetingServiceAsync gsa;
	VerticalPanel mathColumn = new VerticalPanel();
	String mode;

	public plusMinusDivTimes(String uName, String settings,GreetingServiceAsync GSA) {
		super(uName, settings,GSA);

		userName = uName;
		gsa = GSA;
		mode = settings;

		displaySimpleExercise(userName);
		this.selfPanel = mathColumn;
		produceResultButton = submitButton;
	}

	private void displaySimpleExercise(String uName){
		submitButton.setSize("60px", "25px");


		mathColumn.setStyleName("mathColumn");
		HorizontalPanel mathRows = new HorizontalPanel();
		mathColumn.setSpacing(4);

		for (int i = 0;i<20;i++){
			HorizontalPanel singleRow = new HorizontalPanel();
			int tal1 = (int)(20*Math.random());
			int tal2 = (int)(20*Math.random());

			correctAnswers[i] = tal1 + tal2;

			HTML insertHTML = new HTML(tal1+" + " + tal2 + " = " );
			insertHTML.setSize("100px", "25px");
			singleRow.add(insertHTML);

			TextBox insertText = new TextBox();
			insertText.setSize("60px", "23px");
			singleRow.add(insertText);
			singleRow.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
			mathColumn.add(singleRow);

			questList[i] = insertHTML;
			answerList[i] = insertText;
		}
		makeNewQuests();
		HorizontalPanel singleRow = new HorizontalPanel();
		HTML insertHTML = new HTML("Calculate");
		insertHTML.setSize("100px", "25px");
		singleRow.add(insertHTML);

		singleRow.add(submitButton);

		questList[20] = insertHTML;

		mathColumn.add(singleRow);
		mathColumn.setStyleName("ExerciseBasic");
	}

	/**
	 * Renews the questions in the list.
	 */
	private void makeNewQuests(){	
		System.out.println(mode);
		for (int i = 0; i<20; i++){
			if (mode.equals("Plus")){
				int tal1 = (int)((20*Math.random())+1);
				int tal2 = (int)((20*Math.random())+1);
				correctAnswers[i] = tal1 + tal2;
				questList[i].setHTML(tal1+" + " + tal2 + " = " );
			} else if (mode.equals("Minus")){
				int tal1 = (int)((30*Math.random())+1);
				int tal2 = (int)((20*Math.random())+1);
				if (tal1 > tal2){
					correctAnswers[i] = tal1 - tal2;
					questList[i].setHTML(tal1+" - " + tal2 + " = " );			
				} else {
					correctAnswers[i] = tal2 - tal1;
					questList[i].setHTML(tal2+" - " + tal1 + " = " );
				}
			}else if (mode.equals("Div")){
				int tal1 = (int)((10*Math.random())+1);
				int tal2 = ((int)((20*Math.random())+1))*tal1;		//Guarantees integer answers, top term is a factor of the lower one.

				correctAnswers[i] = tal2 / tal1;
				questList[i].setHTML(tal2+" / " + tal1 + " = " );

			}else if (mode.equals("Times")){
				int tal1 = (int)((12*Math.random())+1);
				int tal2 = (int)((12*Math.random())+1);

				correctAnswers[i] = tal1 * tal2;
				questList[i].setHTML(tal1+" * " + tal2 + " = " );
			}
			answerList[i].setValue("");
		}
	}

	private String correctIt(){
		double total = 0;
		double correctA = 0;
		for (int i = 0; i<20;i++){
			total++;
			try{
				if (answerList[i].getText() != ""){
					if (correctAnswers[i] == Integer.parseInt(answerList[i].getText())){
						correctA++;
					} else {
						answerList[i].setValue("FAIL!!");
						//i want to change the background colour of the answer to red, but I have no idea how to do that right now. 
					}
				}else {
					answerList[i].setValue("FAIL!!");
				}
			}catch(Exception e){

			}
		}
		return Integer.toString((int)(correctA/total*100));
	}

	public void draw(){
	}

	public String pushresult(){
		String correctPercent = correctIt(); 
		System.out.println("Pushing result from plusminusdivtimes : " + correctPercent );
		return correctPercent;
	}
}
